<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 11.12.11
 * Time: 1:19
 * To change this template use File | Settings | File Templates.
 */

class lessonContentController extends Core_BaseController
{

    function indexAction($idLesson = -1)
    {

    }

    function addAction()
    {
        $permission = array('admin','guest');
        $auth = $this->checkAuth();
        $allow = $this->userHasPermission($permission);
        $this->twig = $this->getRegistry()->twig;
        $lessonManager = new Lesson_Lesson();
        $messages = array();
        $data = array();
        $role = $this->getAuth()->getUserRoles($auth);
        if ($allow) {
            //var_dump($_POST);
            //validate
            if(empty($_POST))
                 $messages[] = 'Введіть будь-ласка дані';
            if(!is_numeric($_POST['idlesson']))
             $messages[] = 'Виберіть урок!';
            if(strlen($_POST['title'])< 3)
                $messages[]= 'Мінімальна довжина заголовку 4 символа';
            if(strlen($_POST['title']) == 0)
                $messages[] = 'Заголовок не повинен бути пустий';
            if(strlen($_POST['content']) == 0)
                $messages[] = 'Контент не повинен бути пустий';
             //validate

             if(count($messages) == 0){
                 /// add lesson content
                 $idLesson = $_POST['idlesson'];
                 $title = $_POST['title'];
                 $content = $_POST['content'];
                 $lessonManager->addPart($idLesson,$title,$content);
                 $messages = array('Додано');
             }
            $lessons = $lessonManager->getAllLessons();
            $temLessons = array('lessons' => $lessons);
            $messages = array('messages' => $messages,
                               'role' => $role);
            $data = array_merge($data,$temLessons);
            $data = array_merge($data,$auth);
            $data = array_merge($data,$messages);
            //var_dump($data);
            $template = $this->twig->loadTemplate('content_add.html.twig');
            echo $template->render($data);




        } else {
            $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }
    }
}
